package kr.hkit.summury_java.erp.serviceUI;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

import java.awt.GridLayout;

import kr.hkit.myframework.uicomp.InputPanel;
import kr.hkit.summury_java.erp.dao.DaoInterface;
import kr.hkit.summury_java.erp.dto.Department;

import java.awt.BorderLayout;

import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.SQLException;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.SystemColor;

public class PanelDepartmentInputUI extends JPanel implements ActionListener {
	private InputPanel pDeptno;
	private InputPanel pDeptName;
	private InputPanel pFloor;
	private JPanel panel_top;
	private JPanel panel_bottom;
	private JButton btnAdd;
	
	private JPanel pAlarm;
	private JLabel lblAlram;
	
	private DaoInterface<Department> dao;

	/**
	 * Create the panel.
	 */	
	public PanelDepartmentInputUI(String btnName, DaoInterface<Department> dao) {
		initialize();
		btnAdd.setText(btnName);
		this.dao = dao;
	}
	
	public void setDao(DaoInterface<Department> dao) {
		this.dao = dao;
	}

	public InputPanel getpDeptno() {
		return pDeptno;
	}

	public void setpDeptno(InputPanel pDeptno) {
		this.pDeptno = pDeptno;
	}

	public InputPanel getpDeptName() {
		return pDeptName;
	}

	public void setpDeptName(InputPanel pDeptName) {
		this.pDeptName = pDeptName;
	}

	public InputPanel getpFloor() {
		return pFloor;
	}

	public void setpFloor(InputPanel pFloor) {
		this.pFloor = pFloor;
	}

	public JButton getBtnAdd() {
		return btnAdd;
	}

	public void setBtnAdd(JButton btnAdd) {
		this.btnAdd = btnAdd;
	}
	
	public JLabel getLblAlram() {
		return lblAlram;
	}

	public void setLblAlram(JLabel lblAlram) {
		this.lblAlram = lblAlram;
	}

	private void initialize() {
		setLayout(new BorderLayout(0, 20));
		
		panel_top = new JPanel();
		add(panel_top);
		panel_top.setLayout(new GridLayout(0, 1, 0, 30));
		
		pAlarm = new JPanel();
		panel_top.add(pAlarm);
		
		lblAlram = new JLabel("New label");
		lblAlram.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		pAlarm.add(lblAlram);
		
		pDeptno = new InputPanel("부서번호");
		panel_top.add(pDeptno);
		
		pDeptName = new InputPanel("부서명");
		panel_top.add(pDeptName);
		
		pFloor = new InputPanel("위치");
		panel_top.add(pFloor);
		
		panel_bottom = new JPanel();
		add(panel_bottom, BorderLayout.SOUTH);
		
		btnAdd = new JButton("버튼");
		btnAdd.setBackground(SystemColor.inactiveCaption);
		btnAdd.setFont(new Font("맑은 고딕", Font.BOLD, 12));
		btnAdd.addActionListener(this);
		panel_bottom.add(btnAdd);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnAdd) {
			actionPerformedBtnAdd(e);
		}
	}
	
	private boolean inValidCheck() {
		//공백만 체크
			if(pDeptno.getTf().getText().equals("")) {
				pDeptno.getTf().requestFocus();
				return false;
			}
			if(pDeptName.getTf().getText().equals("")) {
				pDeptName.getTf().requestFocus();
				return false;
			}
		
		return true;
		
	}
	private Department getInfo() {
		if (inValidCheck()){
			int dno = Integer.parseInt(pDeptno.getTf().getText().trim());
			String name = pDeptName.getTf().getText();
			int floor = Integer.parseInt(pFloor.getTf().getText().trim());		
			return new Department(dno, name, floor);
		}
			return null; 
		
	}
	
	private void setIniTf() { //텍스트필드 초기화
		pDeptno.getTf().setText("");
		pDeptName.getTf().setText("");
		pFloor.getTf().setText("");
	}
	
	protected void actionPerformedBtnAdd(ActionEvent e) {
		try {	
			if(btnAdd.getText().equals("추가")) {
				Department d = getInfo();
				dao.insertItem(d);
				JOptionPane.showMessageDialog(this, "정상적으로 입력되었습니다.");
				setIniTf();
			}
			
			if(btnAdd.getText().equals("수정")) {
				Department d = getInfo();
				dao.updateItemByNo(d);
				JOptionPane.showMessageDialog(this, "정상적으로 수정되었습니다.");
				setIniTf();
				
			}
			
			if(btnAdd.getText().equals("삭제")) {
				int dno = Integer.parseInt(pDeptno.getTf().getText().trim());
				Department d = new Department(dno);
				dao.deleteItemByNo(d);
				JOptionPane.showMessageDialog(this, "정상적으로 삭제되었습니다.");
				setIniTf();
			}
			
			if(btnAdd.getText().equals("검색")) {
				int dno = Integer.parseInt(pDeptno.getTf().getText().trim());
				Department d = new Department(dno);
				JOptionPane.showMessageDialog(this, dao.searchItem(d));
				setIniTf();
			}
			
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this, "입력을 실패하였습니다. 다시 확인하여 주세요.");
		} catch (NumberFormatException e2) {
			JOptionPane.showMessageDialog(this, "정보를 입력해주세요.");
		}
	}
}
